import os

import pandas as pd


# 文件路径
def file_path(filename):
    data_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    path = os.path.join(data_path, ('data/' + filename))
    return path


class DataOperation:
    def __init__(self, path, sheet=0, file_type='excel'):
        if file_type == 'excel':
            self.data = pd.read_excel(path, sheet_name=sheet)
        elif file_type == 'csv':
            self.data = pd.read_csv(path)

    def get_data_to_dict(self):
        # [{}, {}, {}, ...]
        return [self.data.loc[i].to_dict() for i in self.data.index.values]

    def get_data_to_list(self):
        # [[], [], [], ...]
        return self.data.values.tolist()

    def data_agrument(self):
        li = [tuple(i) for i in self.get_data_to_list()]
        return li


if __name__ == '__main__':
    # 拼接文件路径
    path1 = os.path.abspath(__file__)  # 先获取文件的绝对路径
    path2 = os.path.dirname(path1)  # 获取文件所在目录的绝对路径
    path3 = os.path.dirname(path2)  # 获取文件所在目录的上一级目录,因为所需文件的所在目录就在这个目录下
    # path = 'file:///' + path3 + '/data/lj_data.csv'   # 第一种拼接方式
    path = os.path.join(path3, 'data/lj_data.csv')  # 第二种拼接方式

    data = DataOperation(path)
    # print(data.get_table_to_dict())
    print(data.get_data_to_list())
